<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    require_once(dirname(__FILE__).'/Rain/rain.tpl.class.php');
    class Mytpl extends RainTPL
    {
        private $tpl_file;
        private $cache_lifetime;
        private $cache_directory;
        private $is_cache = FALSE;
        
        function __construct()
        {
            $this->CI =& get_instance();
            $this->CI->config->load('raintpl_config', TRUE);
            $raintpl_config = $this->CI->config->item('raintpl_config');
            
            parent::configure('tpl_dir' , $raintpl_config['raintpl_tpl_dir']);
            parent::configure('tpl_ext' , $raintpl_config['raintpl_tpl_ext']);
            parent::configure('cache_dir' , $raintpl_config['raintpl_cache_dir']);
            $this->cache_directory = $raintpl_config['raintpl_cache_dir'];
            $this->cache_lifetime = $raintpl_config['raintpl_cache_lifetime'];
            
            parent::configure('base_url' , $this->CI->config->site_url());
            $this->set_global('site_url', $this->CI->config->site_url());
        }
        
        public function set_global($var=NULL,$val=NULL)
        {
            if($var && $val){
                $this->assign($var,$val);
            }
        }
        
        public function set_template($tpl=NULL)
        {
            if(strpos($tpl,'.')>0){
                list($tpl,$ext) = explode('.',$tpl);
                parent::configure('tpl_ext' , $ext);
            }
            
            $this->tpl_file = $tpl;
        }
        
        public function set_cache($time=NULL)
        {
            $this->is_cache = TRUE;
            $time ? $this->cache_lifetime = $time : '';
        }
        
        public function clear_cache()
        {
            if( ! $this->is_cache){
                @array_map('unlink',glob($this->cache_directory.$this->tpl_file.".*.rtpl.php"));
            }
        }
        
        public function set_var($var,$val=NULL)
        {
            if(is_array($var)){
                $this->assign($var);
            }else{
                $this->assign($var,$val);
            }
        }
        
        public function display()
        {
            $this->draw($this->tpl_file);
            $this->clear_cache();
        }
        
        public function html()
        {
            $html = $this->draw($this->tpl_file,TRUE);
            $this->clear_cache();
            return $html;
        }
        
        public function write($filename , $folder='')
        {
            
        }
    }
?>